package com.keta.foundation.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.keta.foundation.dao.ModuleDAO;
import com.keta.foundation.dao.RoleDAO;
import com.keta.foundation.entity.Role;
import com.keta.foundation.entity.RolePermission;
import com.keta.foundation.entity.User;
import com.keta.foundation.exception.ExistedException;
import com.keta.foundation.service.OrganizationRoleService;
import com.keta.foundation.service.RolePermissionService;
import com.keta.foundation.service.RoleService;
import com.keta.foundation.service.UserRoleService;
import com.keta.foundation.util.dwz.Page;
import com.keta.foundation.util.dwz.PageUtil;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleDAO roleDAO;

	@Autowired
	private RolePermissionService rolePermissionService;

	@Autowired
	private UserRoleService userRoleService;

	@Autowired
	private OrganizationRoleService organizationRoleService;

	public Role get(Long roleId) {
		return roleDAO.findOne(roleId);
	}

	public List<Role> getAll() {
		return roleDAO.findAll();
	}

	public List<Role> getAll(Page page) {
		org.springframework.data.domain.Page<Role> springPage = roleDAO.findAll(PageUtil.createPageable(page));
		page.setTotalPage(springPage.getTotalPages());
		page.setTotalCount(springPage.getTotalElements());
		return roleDAO.findAll();
	}

	@Override
	public void saveOrUpdate(Role role) {

		if (null == role.getId()) {
			Role oldRole = roleDAO.getByName(role.getName());
			if (null != oldRole) {
				throw new ExistedException(role.getName() + "角色已经存在");
			} else {
				Role curRole = roleDAO.save(role);
				// 保存角色权限
				for (RolePermission rolePermission : role.getRolePermissions()) {
					rolePermission.setRole(curRole);
					rolePermissionService.save(rolePermission);
				}
			}

		} else {
			// 保存角色本身
			Role oldRole = roleDAO.save(role);

			List<RolePermission> newRList = new ArrayList<RolePermission>();
			List<RolePermission> delRList = new ArrayList<RolePermission>();

			List<RolePermission> hasRolePermissions = rolePermissionService.getByRoleId(role.getId());
			for (RolePermission rolePermission : role.getRolePermissions()) {
				if (rolePermission.getId() == null && rolePermission.getPermission() != null) {
					rolePermission.setRole(oldRole);
					newRList.add(rolePermission);
				} else if (rolePermission.getId() != null && rolePermission.getPermission() == null) {
					for (RolePermission rp : hasRolePermissions) {
						if (rp.getId().equals(rolePermission.getId())) {
							delRList.add(rp);
							break;
						}
					}
				}
			}

			rolePermissionService.save(newRList);
			rolePermissionService.delete(delRList);
		}
	}

	@Override
	public void delete(Long[] ids) {
		// 删除角色时，同时删除分配给用户的角色 和 分配给组织的角色
		for (Long id : ids) {
			// 删除角色权限
			List<RolePermission> rolePermissions = rolePermissionService.getByRoleId(id);
			rolePermissionService.delete(rolePermissions);

			// 删除分配给用户的角色
			userRoleService.delByRole(id);

			// 删除分配给组织的角色
			organizationRoleService.delByRole(id);

			roleDAO.delete(id);
		}
	}
}
